<template>
  <el-card
    style="margin:0px 0px 20px 0px"
    shadow="always"
    body-style="background:#fafafa"
  >
    <div
      slot="header"
      class="clearfix"
    >
      <span>待办任务</span>
    </div>
    <el-table
      ref="table"
      v-loading="loading"
      :data="taskList"
    >
      <el-table-column label="任务名称" prop="name" width="200" />
      <el-table-column label="任务类型">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_business_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" width="100">
          <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_task_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-card>
</template>

<script>
import { listTask, delTask } from '@/api/task/task'

export default {
  name: 'TaskList',
  dicts: ['sys_business_type', 'sys_task_status'],
  props: {
    corpName: { // 不为空说明是在客户详情页，没有搜索等功能
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      selected: [],
      // 显示搜索条件
      showSearch: !this.corpName,
      // 总条数
      total: 0,
      // 角色表格数据
      taskList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        corpName: this.corpName || undefined,
        name: undefined,
        type: undefined,
        status: "0,1"
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listTask(this.queryParams).then(response => {
        this.taskList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selected.map(v => v.id)
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delTask(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
        this.$refs.table.clearSelection()
        this.selected = []
      }).catch(() => {})
    },
    // 导出过滤后的企业数据
    handleExport() {
      this.download('security/taskInfo/export', this.queryParams, `任务概要信息表_${new Date().getTime()}.xlsx`)
    },
    goDetailPage(row) {
      this.$router.push({
        name: 'TaskDetail',
        params: {
          taskId: row.id,
          type: row.type || 'environment'
        }
      })
    }
  }
}
</script>
